package com.ugoodtech.mdcc.core.repository;

import com.ugoodtech.mdcc.core.domain.UserProfile;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserProfileRepository extends PagingAndSortingRepository<UserProfile, Long>
        , QueryDslPredicateExecutor<UserProfile> {

    List<UserProfile> findByRealNameAndDeletedFalse(String name);

    @Query("from UserProfile where user_id=:userId")
    UserProfile findByUserId(@Param("userId") Long userId);

    @Query("from UserProfile where user_id in :userId")
    List<UserProfile> findByUserId(@Param("userId") List<Long> userId);

    @Query("from UserProfile where serialNumber=:serialNumber and deleted=false and enabled=true")
    UserProfile findBySerialNumber(@Param("serialNumber") String serialNumber);

    @Query("from UserProfile where mobilePhone=:phone and deleted=false and enabled=true")
    UserProfile findByMobilePhone(@Param("phone") String phone);

    @Query("from UserProfile where serialNumber=:serialNumber and realName=:name and deleted=false and enabled=true")
    UserProfile findBySerialNumberAndName(@Param("serialNumber") String serialNumber, @Param("name") String name);

    @Query("from UserProfile where telephone=:phone and deleted=false and enabled=true")
    UserProfile findByTelephone(@Param("phone") String phone);

    @Query("from UserProfile where telephone=:phone and deleted=false and enabled=true")
    List<UserProfile> findUsersByTelephone(@Param("phone") String phone);

    @Query("from UserProfile where deleted=false and enabled=true and serialNumber=:serialNumber and id!=:userProfileId")
    UserProfile isSameSerialNumber(@Param("userProfileId") Long userProfileId, @Param("serialNumber") String serialNumber);

    @Query("from UserProfile where deleted=false and enabled=true and telephone=:telephone and id!=:userProfileId")
    UserProfile isSameTelephone(@Param("userProfileId") Long userProfileId, @Param("telephone") String telephone);

    @Query(value = "SELECT user_id FROM user_authorities WHERE authority_id=:authId", nativeQuery = true)
    List<Object> getUserByAuthoritieId(@Param("authId") Long authId);

    // @Query(value = "SELECT user_id, real_name FROM user_profile WHERE user_id in=:authId",
    //     nativeQuery =
    //     true)
    // List<UserProfile> getRealName(@Param("userIdList") List<Long> userIdList);

}
